<template>
  <div v-loading="loading" class="dashboard-container">
    <div class="app-container">
      <!-- 组织架构的内容 -->
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->
        <tree-tools :tree-node="company" :is-root="true" @addDepts="addDepts" @delDepts="getDepartments" />
      </el-card>
      <!-- 树形数据 -->
      <el-tree :data="departs" :props="defaultProps" :default-expand-all="true">
        <tree-tools slot-scope="{data}" :tree-node="data" @delDepts="getDepartments" @addDepts="addDepts" @editDepts="editDepts" />
      </el-tree>
    </div>
    <add-dept ref="addDept" :show-dialog.sync="showDialog" :tree-node="node" @delDepts="getDepartments" />
  </div>

</template>

<script>
import treeTools from './components/tree-tools'
import { getDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
import AddDept from './components/add-dept'

export default {
  components: {
    treeTools,
    AddDept
  },
  data () {
    return {
      company: {},
      departs: [],
      defaultProps: {
        label: 'name'
      },
      loading: false, // 给数据添加加载进度条
      showDialog: false, // 默认不显示弹层
      node: null // 当前点击的节点
    }
  },
  created () {
    this.getDepartments()
  },
  methods: {
    async getDepartments () {
      this.loading = true
      const result = await getDepartments()
      // console.log(result);
      this.company = { name: result.companyName, manager: '负责人', id: '' }
      this.departs = tranListToTreeData(result.depts, '')
      this.loading = false
    },
    addDepts (node) {
      this.showDialog = true
      this.node = node
    },
    editDepts (node) {
      this.showDialog = true
      this.node = node
      this.$refs.addDept.getDepartDetail(node.id)
    }

  }

}
</script>

<style scoped>
.tree-card {
     /* padding: 30px  140px; */
     font-size:14px;
  }
 .el-col-24 {
    width: 100vh;
    /* margin-left: 9px; */
}

</style>
